<?php
if (!defined("IPFILE")) define ("IPFILE", "../include/QQWry.dat");

function bin2dec($strBin) {
    $intLen = strlen($strBin);
    for (
        $i = 0, $intBase = 1, $intResult = 0;
        $i < $intLen; $i++, $intBase *= 256
    ) $intResult += ord($strBin{$i}) * $intBase;
    return $intResult;
}
function ip2addr($intIp) {
    $arrUnknown = array(
        "region" => "(unknown)",
        "address" => "(unknown)"
    );
    $fileIp = fopen(IPFILE, "rb");
    if (!$fileIp) return 1;
    $strBuf = fread($fileIp, 4);
    $intFirstRecord = bin2dec($strBuf);
    $strBuf = fread($fileIp, 4);
    $intLastRecord = bin2dec($strBuf);
    $intCount = floor(($intLastRecord - $intFirstRecord) / 7);
    if ($intCount < 1) return 2;
    $intStart = 0;
    $intEnd = $intCount;
    while ($intStart < $intEnd - 1) {
        $intMid = floor(($intStart + $intEnd) / 2);
        $intOffset = $intFirstRecord + $intMid * 7;
        fseek($fileIp, $intOffset);
        $strBuf = fread($fileIp, 4);
        $intMidStartIp = bin2dec($strBuf);
        if ($intIp == $intMidStartIp) {
            $intStart = $intMid;
            break;
        }
        if ($intIp > $intMidStartIp) $intStart = $intMid;
        else $intEnd = $intMid;
    }
    $intOffset = $intFirstRecord + $intStart * 7;
    fseek($fileIp, $intOffset);
    $strBuf = fread($fileIp, 4);
    $intStartIp = bin2dec($strBuf);
    $strBuf = fread($fileIp, 3);
    $intOffset = bin2dec($strBuf);
    fseek($fileIp, $intOffset);
    $strBuf = fread($fileIp, 4);
    $intEndIp = bin2dec($strBuf);
    if ($intIp < $intStartIp || $intIp > $intEndIp) return $arrUnknown;
    $intOffset += 4;
    while (($intFlag = ord(fgetc($fileIp))) == 1) {
        $strBuf = fread($fileIp, 3);
        $intOffset = bin2dec($strBuf);
        if ($intOffset < 12) return $arrUnknown;
        fseek($fileIp, $intOffset);
    }
    switch ($intFlag) {
        case 0:
            return $arrUnknown;
            break;
        case 2:
            $intOffsetAddr = $intOffset + 4;
            $strBuf = fread($fileIp, 3);
            $intOffset = bin2dec($strBuf);
            if ($intOffset < 12) return $arrUnknown;
            fseek($fileIp, $intOffset);
            while (($intFlag = ord(fgetc($fileIp))) == 2 || $intFlag == 1) {
                $strBuf = fread($fileIp, 3);
                $intOffset = bin2dec($strBuf);
                if ($intOffset < 12) return $arrUnknown;
                fseek($fileIp, $intOffset);
            }
            if (!$intFlag) return $arrUnknown;
            $arrAddr = array(
                "region" => chr($intFlag)
            );
            while (ord($c = fgetc($fileIp))) $arrAddr["region"] .= $c;
            fseek($fileIp, $intOffsetAddr);
            while (($intFlag = ord(fgetc($fileIp))) == 2 || $intFlag == 1) {
                $strBuf = fread($fileIp, 3);
                $intOffset = bin2dec($strBuf);
                if ($intOffset < 12) {
                    $arrAddr["address"] = "(unknown)";
                    return $arrAddr;
                }
                fseek($fileIp, $intOffset);
            }
            if (!$intFlag) {
                $arrAddr["address"] = "(unknown)";
                return $arrAddr;
            }
            $arrAddr["address"] = chr($intFlag);
            while (ord($c = fgetc($fileIp))) $arrAddr["address"] .= $c;
            return $arrAddr;
            break;
        default:
            $arrAddr = array("region" => chr($intFlag));
            while (ord($c = fgetc($fileIp))) $arrAddr["region"] .= $c;
            while (($intFlag = ord(fgetc($fileIp))) == 2 || $intFlag == 1) {
                $strBuf = fread($fileIp, 3);
                $intOffset = bin2dec($strBuf);
                if ($intOffset < 12) {
                    $arrAddr["address"] = "(unknown)";
                    return $arrAddr;
                }
                fseek($fileIp, $intOffset);
            }
            if (!$intFlag) {
                $arrAddr["address"] = "(unknown)";
                return $arrAddr;
            }
            $arrAddr["address"] = chr($intFlag);
            while (ord($c = fgetc($fileIp))) $arrAddr["address"] .= $c;
            return $arrAddr;
    }
}
?>